<#include "/java_copyright.include">
<#assign className = table.className>   
<#assign classNameLower = className?uncap_first> 
package ${basepackage}.service.impl;

import java.util.List;
import java.util.ArrayList;
import java.lang.RuntimeException;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import my.mybatis.common.QueryParams;
import my.mybatis.common.BaseService;
import my.mybatis.common.exception.BaseException;
import my.mybatis.common.request.BaseRequestParam;

import org.springframework.transaction.annotation.Transactional;

import my.mybatis.common.util.QueryBuildFactory;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import ${basepackage}.dao.${className}Dao;
import ${basepackage}.service.${className}Service;
import ${basepackage}.dto.${className};

/**
<#include "/java_description.include">
 */
@Service
@Transactional(rollbackFor = {BaseException.class,RuntimeException.class})
public class ${className}ServiceImpl extends BaseService<${className}> implements ${className}Service {

	@Resource
	${className}Dao ${classNameLower}Dao;
	
	/** 
	 * 根据ID查询${className}
	 */
	@Override
	 public ${className} findById(${table.idColumn.javaType} id){
		 return ${classNameLower}Dao.findById(id);
	 }
	 
   /** 
    *  根据ID删除${className}
    */
	@Override
    public int deleteById(${table.idColumn.javaType} id){
    	return ${classNameLower}Dao.deleteById(id);
    }
	
	/** 
	 * 根据ID集合删除${className}
	 **/
	@Override
	public int deleteByQueryParams(QueryParams<${className}> queryParams){
		return ${classNameLower}Dao.deleteByQueryParams(queryParams);
	}
    
	/** 
	 * 更新${className}
	 **/
    @Override
    public int update(${className} ${classNameLower}){
		return ${classNameLower}Dao.update(${classNameLower});
    }
	
	/** 
	 * 根据条件queryParams批量更新${className}
	 * return 数据库受影响的行数
	 * @param ${classNameLower}
	 * @param queryParams
	 **/	
    public int updateByQueryParams(${className} ${classNameLower},QueryParams<${className}> queryParams){
		return ${classNameLower}Dao.updateByQueryParams(${classNameLower},queryParams);
	}
    
    /** 
	 * 创建${className}
	 **/
    @Override
	public ${className} save(${className} ${classNameLower}){
		${classNameLower}Dao.save(${classNameLower});
		return ${classNameLower};
	}
	
	/**
	 * 批量创建${className}
	 * @param list
	 */
	@Override
	public int saveBatch(List<${className}> list){
		return ${classNameLower}Dao.saveBatch(list);
	}
    
	/**
	 * 带条件查询，条件可以为null，既没有条件；返回list对象集合
	 */
	@Override
	public List<${className}> list(BaseRequestParam<${className}> baseRequestParam){
		List<${className}> list =  ${classNameLower}Dao.findAll(getQueryParams(baseRequestParam));
		return list;
	}
	
	/**
	 * 分页查询
	 **/
	@Override
	public PageInfo<${className}> listPage(BaseRequestParam<${className}> baseRequestParam){
		PageHelper.startPage(baseRequestParam.getPageNo(),baseRequestParam.getPageSize());
		List<${className}> list =  ${classNameLower}Dao.findAll(getQueryParams(baseRequestParam));
		PageInfo<${className}>  page = new PageInfo<${className}>(list);
		return page;
	}
	
	@Override
	public List<${className}> findByQueryParams(QueryParams<${className}> queryParams){
		List<${className}> list = ${classNameLower}Dao.findAll(queryParams);
		return list == null ? new ArrayList<${className}>() : list;
	}
	
	/**
	 * 直接构造排序条件
	 */
	@Override
	protected void addOrderParams()
	{
		// 此处可能有需求加默认排序，用时按需求自己添加，添加方式如下：orderBy:dto字段名，不要直接写数据库映射字段名
		//addOrderParam(QueryBuildFactory.createOrderParam().orderBy("id").ascOrDesc(OrderParam.ORDER_DESC).build());
		// 或者使用方式(传入参数属性值  key，排序规则)
		//addOrderParam("id", OrderParam.ORDER_DESC);
	}

	/**
	 * 通过查询对象来设置条件value
	 */
	@Override
	protected void addQueryParamsByParam(Object param)
	{
		if(param != null)
		{
			if(param instanceof ${className})
			{
				${className} ${classNameLower} = (${className}) param;
				// key : dto字段名，不要直接写数据库映射字段名；   operator：操作符，参见类OperatorConventor；   value : 数据值，${classNameLower}.getXxx()
				//if(${classNameLower}.getId() != null)
					//addQueryParam(QueryBuildFactory.createQueryParam().key("id").operator(OperatorConventor.EQ).value(${classNameLower}.getId()).build());
				//或者使用方式(传入参数 key,operator,value)
					//addQueryParam("id",OperatorConventor.EQ,${classNameLower}.getId());
			}
			else
			// 通过自定义的参数构建查询参数（从controller层传入的BaseRequestParam中的param属性可以为任何类型）
			// key 必须要对应为要查询的数据库对应实体的属性名
			{
				
			}
		}
	}
	
	@Override
	protected Class getClazz() {
		return ${className}.class;
	}
}
